package com.valuegroup.smarttodo.util;


import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.valuegroup.smarttodo.SmartTodoApp;
import com.valuegroup.smarttodo.data.enums.GoalSortTypeEnum;
import com.valuegroup.smarttodo.data.enums.TaskSortTypeEnum;
import com.valuegroup.smarttodo.data.vo.AppCustomCfg;


public class Cfg {

    public static final String CONFIG = "SmartTodoCfg";

    public static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
    }

    public static void clear(Context context) {
        getSharedPreferences(context).edit().clear().apply();
    }

    public static void write(Context context, String key, String val) {
        getSharedPreferences(context).edit().putString(key, val).apply();
    }

    public static void write(Context context, String key, boolean val) {
        getSharedPreferences(context).edit().putBoolean(key, val).apply();
    }

    public static void write(Context context, String key, int val) {
        getSharedPreferences(context).edit().putInt(key, val).apply();
    }

    public static void write(Context context, String key, long val) {
        getSharedPreferences(context).edit().putLong(key, val).apply();
    }

    public static String read(Context context, String key, String def) {
        return getSharedPreferences(context).getString(key, def);
    }

    public static boolean read(Context context, String key, boolean def) {
        return getSharedPreferences(context).getBoolean(key, def);
    }

    public static int read(Context context, String key, int def) {
        return getSharedPreferences(context).getInt(key, def);
    }

    public static long read(Context context, String key, long def) {
        return getSharedPreferences(context).getLong(key, def);
    }


    public static void saveCfg(Context context, AppCustomCfg cfg) {
        if (cfg != null) {
            Cfg.write(context, SmartTodoApp.APP_CUSTOM_CFG, JSON.toJSONString(cfg));
        }
    }

    public static AppCustomCfg getCfg(Context context) {
        AppCustomCfg cfg = null;

        try {
            String cfgStr = read(context, SmartTodoApp.APP_CUSTOM_CFG, "");
            if (!TextUtils.isEmpty(cfgStr)) {
                cfg = JSON.toJavaObject(JSON.parseObject(cfgStr), AppCustomCfg.class);
            }
        } catch (Exception e) {

        }

        if (cfg == null) {
            System.out.println(">>>>>>>未找到有效配置，生成默认配置");
            cfg = new AppCustomCfg();
            cfg.setFirstRun(false); // 记录首次运行

            cfg.setTaskHideFinish(false);
            cfg.setTaskHideTimeout(false);
            cfg.setTaskSortDesc(false);
            cfg.setTaskSortType(TaskSortTypeEnum.NORMAL.getSortType());

            cfg.setRethinkEditModel(false);

            cfg.setGoalHideFinish(false);
            cfg.setGoalSortDesc(false);
            cfg.setGoalSortType(GoalSortTypeEnum.NORMAL.getSortType());

            cfg.setCurriculumHideFinish(false);
            cfg.setIdealityHideFinish(false);
            cfg.setHabitHideFinish(false);

            Cfg.write(context, SmartTodoApp.APP_CUSTOM_CFG, JSON.toJSONString(cfg));
        }
        return cfg;
    }
}
